package com.echat.serviceapigateway.security.controller;

import com.echat.commonlib.exception.exc.ValidateFailedException;
import com.echat.serviceapigateway.security.common.HttpResult;
import com.echat.serviceapigateway.security.constant.ControllerMapping;
import com.echat.serviceapigateway.security.model.authentication.User;
import com.echat.serviceapigateway.security.model.authorization.vo.PermissionNode;
import com.echat.serviceapigateway.security.model.network.AccessIp;
import com.echat.serviceapigateway.security.service.IpWhiteListService;
import com.echat.serviceapigateway.security.service.ResourceService;
import com.echat.serviceapigateway.security.service.UserService;
import com.echat.serviceapigateway.security.tool.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.Collections;
import java.util.List;

/**
 * Created by chenmohan on 2018/08/30
 *
 * @description 普通用户Controller
 */
@RestController
@RequestMapping(ControllerMapping.UserController.uri)
public class UserController {

    @Autowired
    UserService userService;

    @Autowired
    IpWhiteListService ipWhiteListService;

    @Autowired
    ResourceService resourceService;

    /**
     * 创建用户
     * @param user
     * @return
     */
    @RequestMapping(ControllerMapping.UserController.add_user)
    public HttpResult<User> createUser(User user) {
        User u = userService.createUser(user);
        return HttpResult.success(u);
    }

    /**
     * 根据用户名查询用户
     * @param user
     * @return
     */
    @RequestMapping(ControllerMapping.UserController.get_user_by_pk)
    public HttpResult<User> getUserByPk(User user) {
        User u = userService.getUserByPrimaryKey(user.getUserName());
        return HttpResult.success(u);
    }

    /**
     * 直接跳转到 一般用户基本信息页面
     *
     * @param userName
     * @return
     */
    @RequestMapping("/{userName}")
    public ModelAndView user(@PathVariable("userName") String userName, HttpSession session) {
        if (StringUtils.isEmpty(userName)) {
            throw new ValidateFailedException("userName不能为空");
        }
        User user = userService.getUserByPrimaryKey(userName);
        if (user == null) {
            throw new ValidateFailedException("无效的userName");
        }
        AccessIp ip = ipWhiteListService.getAccessIpByUserId(user.getId());
        ModelAndView mv = new ModelAndView("user/userInfo_simple.html");
        mv.addObject("user", user);
        mv.addObject("ip", ip.getIps());
        session.setAttribute("userName", userName);
        return mv;
    }

    /**
     * 获取当前登录用户的权限树,如果该用户没有登录,那么返回空集合
     *
     * @return
     */
    @RequestMapping("/auth")
    public List<PermissionNode> auth(HttpSession session) {
        String userName = (String) session.getAttribute("userName");
        if (userName == null) {
            return Collections.emptyList();
        } else {
            User user = userService.getUserByPrimaryKey(userName);
            return resourceService.getAuthTree(user.getId());
        }

    }
}
